package com.biz.primus.ms.promotion.service.activity;

import com.biz.primus.base.enums.CommonStatus;
import com.biz.primus.base.exception.utils.AssertUtils;
import com.biz.primus.model.promotion.exception.PromotionExceptionType;
import com.biz.primus.model.promotion.vo.ActivityVo;
import com.biz.primus.ms.promotion.dao.redis.ActivityItemRedisDao;
import com.biz.primus.ms.promotion.dao.redis.ActivityRedisDao;
import com.biz.primus.ms.promotion.dao.ro.ActivityItemRo;
import com.biz.primus.ms.promotion.dao.ro.ActivityRo;
import com.biz.primus.ms.promotion.trans.ActivityItemRo2ActivityItemVoMapper;
import com.biz.primus.ms.promotion.trans.ActivityRo2ActivityVoMapper;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 活动Service
 *
 * @author david-liu
 */
@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityRedisDao activityRedisDao;

    @Autowired
    private ActivityItemRedisDao activityItemRedisDao;

    @Override
    public ActivityVo getActivity(Long activityId) {
        AssertUtils.notNull(activityId, PromotionExceptionType.ILLEGAL_PARAMETER);
        ActivityRo activityRo = activityRedisDao.findOne(activityId);
        AssertUtils.notNull(activityRo, PromotionExceptionType.ILLEGAL_STATE);
        List<ActivityItemRo> activityItems = activityItemRedisDao.getActivityItems(activityId);
        AssertUtils.isTrue(activityRo.getStatus() == CommonStatus.DISABLE.getValue(), PromotionExceptionType.ACTIVITY_IS_DISABLED);
        AssertUtils.isTrue(activityRo.isActivityInValidPeriod(), PromotionExceptionType.ACTIVITY_NOT_IN_VALID_PERIOD);
        ActivityRo2ActivityVoMapper mapper = new ActivityRo2ActivityVoMapper();
        return mapper.doMap(activityRo, new ActivityItemRo2ActivityItemVoMapper(), activityItems.stream().filter(activityItemRo -> Objects.nonNull(activityItemRo) && activityItemRo.getStatus() == CommonStatus.ENABLE.getValue()).collect(Collectors.toList()));
    }
}
